package com.example.take_out_admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.take_out_admin.common.Result;
import com.example.take_out_admin.entity.Employee;
import com.example.take_out_admin.exception.ResponseEnum;
import com.example.take_out_admin.service.EmployeeService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2024-06-27
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private HttpServletRequest request;

    @PostMapping("login")
    public Result<String> login(@RequestBody  Employee employee){
        String token = employeeService.login(employee);
        return Result.success(ResponseEnum.SUCCESS,token);
    }

    //page 分页查询 int page, int pageSize, String name
    @GetMapping("/page")
    public Result<Page<Employee>> page(@RequestParam int page, @RequestParam int pageSize, @RequestParam String name){
        Page<Employee> employeePage = employeeService.getEmployeePage(page, pageSize, name);
        return Result.success(ResponseEnum.SUCCESS,employeePage);
    }

    //save 保存员工Employee : 验证token,找到创建人和更新人，设置--每次都是保存不同的
    @PostMapping("/save")
    public Result<Object> saveEmployee(@RequestBody Employee employee){

        employeeService.saveEmployee(employee);

        return Result.success();
    }


   //update更新员工：验证Token-找到更新人--update
    @PutMapping("/update")
    public Result<Object> updateEmployee(@RequestBody Employee employee){
        employeeService.updateEmployee(employee);
        return Result.success();
    }

}
